import {
	baseUrl
} from "../../config";
import {
	getToken
} from "../../utils/auth";
/**
 * 上传文件
 * @param {String} filePath 文件路径
 * @returns {Promise} 返回 Promise 对象
 */

const header = {
	// 'Content-Type': 'multipart/form-data',
	// 'Authorization': 'Bearer ' + getToken() 
	'App-Authorization': getToken()
};

export function uploadfileFn(filePath, type = false) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: type ? `${baseUrl}/file/ocr/upload` : `${baseUrl}/file/upload`, 
			filePath: filePath,
			name: 'file',
			header: header,
			formData: {},
			success: (uploadRes) => {
				console.log('uploadRes', uploadRes);
				handleSuccess(resolve, uploadRes);
			},
			fail: (uploadErr) => {
				console.log('uploadErr', uploadErr);
				handleFailure(reject, uploadErr);
			}
		});
	});
}

/**
 * 处理上传成功响应
 * @param {Function} resolve 成功回调函数
 * @param {Object} uploadRes 上传响应对象
 */
function handleSuccess(resolve, uploadRes) {
	console.log(uploadRes, "uploadRes");
	if (uploadRes.statusCode === 200) {
		resolve(JSON.parse(uploadRes.data));
	} else {
		handleFailure(resolve, uploadRes);
	}
}

/**
 * 处理上传失败响应
 * @param {Function} reject 失败回调函数
 * @param {Object} err 错误对象
 */
function handleFailure(reject, err) {
	reject(err);
}